﻿using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IsreversI.GUI
{
    class MainWindow : Form
    {
        #region membervars
        #region GUI elements
        private BoardControl board;
        private ScoreControl score;
        private MenuStrip menu;
        #endregion

        GameLogic.GameStateView stateView;
        bool resizeInProgress = false;
        Size boardPadding = new Size(80, 160);
        #endregion

        #region events
        #endregion

        #region properties
        #endregion

        #region constructors
        public MainWindow()
        {
            this.InitialiseComponents();
        }

        private void InitialiseComponents()
        {
            // Create some objects to ensure that we don't get any duplicates.
            // Duplicates wouldn't hurt much (except for the model), but it's
            // slightly more convenient this way as board model changes are
            // handled in a simpler way.
            GameLogic.BoardModel model = new GameLogic.BoardModel(new Size(6, 6));
            this.stateView = new GameLogic.GameStateView(model);
            GameLogic.GameStateController stateController = new GameLogic.GameStateController(stateView);
            Visualisation.BoardVisualView visualView = new Visualisation.BoardVisualView(stateView);
            this.board = new BoardControl(visualView, stateController);
            this.score = new ScoreControl(stateView);
            this.menu = new MenuStrip();

            // The board
            this.board.Size = new Size(320, 320);
            this.board.Location = new Point(40, 150);

            // The score
            this.score.Location = new Point(40, 30); 

            // The menu
            ToolStripDropDownItem start = new ToolStripDropDownButton("Start");
            start.DropDownItems.Add("New Game", null, this.openNewGameDialog);
            start.DropDownItems.Add("Exit", null, this.exitApplication);
            ToolStripButton help = new ToolStripButton("Hint", null, showHints);
            this.menu.RenderMode = ToolStripRenderMode.System;
            this.menu.Items.Add(start);
            this.menu.Items.Add(help);

            // This form
            this.Text += "IsreversI";
            this.ResizeBegin += this.handleBeginResize;
            this.Resize += this.handleResizeInProgress;
            this.ResizeEnd += this.handleEndResize;
            this.MinimumSize = this.ClientSize = new Size(400, 480);            

            this.Controls.Add(this.board);
            this.Controls.Add(this.score);
            this.Controls.Add(this.menu);
        }
        #endregion

        #region event handlers

        /// <summary>
        /// handleBeginResize is fired in response to the ResizeBegin event of
        /// the main form.  handleBeginResize fires once when the border of the form
        /// is dragged.  It does not fire when the 'maximize' button of the
        /// form is clicked. 
        /// </summary>
        private void handleBeginResize(object sender, EventArgs e)
        {
            this.resizeInProgress = true;
        }

        /// <summary>
        /// handleResizeInProgress is fired in response to the Resize event
        /// of the main form.  handleResizeInProgress fires zero or more times
        /// when the border of the form is dragged.  It fires once when the
        /// 'maximize' button of the form is clicked.
        /// </summary>
        private void handleResizeInProgress(object sender, EventArgs e)
        {
            if (this.resizeInProgress) // Never do anything if we're already resizing
                return;
            this.performResize();
        }

        /// <summary>
        /// Fires once when the resize of the form has ended if a handleBeginResize
        /// event has been raised.  Resets the flag and resizes the image.
        /// </summary>
        private void handleEndResize(object sender, EventArgs e)
        {
            this.resizeInProgress = false;
            this.performResize();
        }

        private void openNewGameDialog(object sender, EventArgs e)
        {
            NewGameDialog dialog = new NewGameDialog(stateView);
            dialog.ShowDialog();
        }

        private void exitApplication(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void showHints(object sender, EventArgs e)
        {            
            board.EnableDecoration = !board.EnableDecoration;  
        }
        #endregion

        #region public methods
        #endregion

        #region private methods
        /// <summary>
        /// Logic for actually resizing the form.
        /// </summary>
        private void performResize()
        {
            this.board.Size = this.ClientSize - this.boardPadding;
        }
        #endregion
    }
}
